package com.kawaiior.asoulworld.item.weapon;

import com.kawaiior.asoulworld.IdlFramework;
import com.kawaiior.asoulworld.item.ItemASWeapon;
import com.kawaiior.asoulworld.util.EntityUtil;
import com.kawaiior.asoulworld.util.IDLGeneral;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.List;

public class ItemKiraIronStick extends ItemASWeapon {
    public ItemKiraIronStick(String name, ToolMaterial material) {
        super(name, material);
        this.useable = true;
        this.attackDamage = 6f;
        this.base_cd = 3f;
        this.base_power = 10f;
        this.base_range = 8f;
    }

    @Override
    public int getMaxDamage(ItemStack stack) {
        return 2048;
    }

    @Override
    public EnumAction getItemUseAction(ItemStack stack)
    {
        return EnumAction.BLOCK;
    }

    public void onCreatureStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
    {
        //IdlFramework.Log("onCreatureStoppedUsing");
        Vec3d basePos = entityLiving.getPositionVector();
        float range = getRange(stack);
        List<EntityLivingBase> entities = worldIn.getEntitiesWithinAABB(EntityLivingBase.class, IDLGeneral.ServerAABB(basePos.addVector(-range, -range, -range), basePos.addVector(range, range, range)));

        IdlFramework.Log("ItemKiraIronStick found %d targets", entities.size());

        EntityPlayer entityplayer = (EntityPlayer) entityLiving;

        for (EntityLivingBase target: entities) {
            if (target.isEntityEqual(entityplayer)) {
                continue;
            }
            if (worldIn.isRemote) {
                EntityUtil.SpawnParticleAround(target, EnumParticleTypes.EXPLOSION_NORMAL, 3);
            } else {
                float d = this.getPower(stack);
                target.attackEntityFrom(DamageSource.causePlayerDamage(entityplayer), base_power);
                stack.damageItem(4, entityLiving);
            }
        }

        entityLiving.playSound(SoundEvents.BLOCK_ANVIL_LAND, 1f, 0.8f);
    }
}
